﻿# encoding:utf-8
# 访问数据库 群与用户关系表
from wxpyImport import *
from db.wxpyDbBase import *

def dbGetGroupUser(dbd, groupPuid, userPuid, resetDb=False):
    sql = "select * from group_user where group_puid = %s and user_puid = %s order by create_time desc limit 1"

    results = None
    try:
        cursor = getCursorColName(dbd, resetDb)
        cursor.execute(sql,(groupPuid, userPuid))
        results = cursor.fetchone()
        cursor.close()
    except Exception as e:
        dealException()
        logError('dbGetGroupUse err: '+str(e))
        if not resetDb: # 防止死循环
            return dbGetGroupUser(dbd, groupPuid, userPuid, True)
    
    return results

# 取组内所有成员
def dbGetGroupUsers(dbd, groupPuid, resetDb=False):
    sql = "select * from group_user where group_puid = %s order by create_time desc"

    results = None
    try:
        cursor = getCursorColName(dbd, resetDb)
        cursor.execute(sql,(groupPuid))
        results = cursor.fetchall()
        cursor.close()
    except Exception as e:
        dealException()
        logError('dbGetGroupUsrs err: '+str(e))
        logError('dbGetGroupUsrs err2: '+str(groupPuid))
        if not resetDb: # 防止死循环
            return dbGetGroupUsers(dbd, groupPuid, True)
    
    return results

# 插入一条新的,返回是否成功
def dbAddGroupUser(dbd, groupPuid, userPuid, resetDb=False):
    if groupPuid is None or ''==groupPuid:
        logError('dbAddGroupUse groupPuid empty')
        return False
    if userPuid is None or ''==userPuid:
        logError('dbAddGroupUse userPuid empty')
        return False

    sql = "INSERT INTO group_user (group_puid, user_puid) VALUES(%s, %s)"

    try:
        cursor = getCursor(dbd, resetDb)
        # 存大块聊天记录
        cursor.execute(sql,(groupPuid, userPuid))
        dbd.db.commit()
        cursor.close()
    except Exception as e:
        dealException()
        logError('dbAddGroupUse err: '+str(e))
        if not resetDb: # 防止死循环
            return dbAddGroupUser(dbd, groupPuid, userPuid, True)
    else:
        logInfo('dbAddGroupUse success: ' + groupPuid)
        return True

    return False

# 删除，并将删除数据移动到 group_user_left 表中
# 有事务，不成功回滚
def dbDelGroupUser(dbd, groupPuid, userPuid):
    groupUser = dbGetGroupUser(dbd, groupPuid, userPuid)
    if groupUser is None:
        logError('dbDelGroupUse not exist grouPuid: '+groupPuid+' userPuid: '+userPuid)
        return False

    sqlAdd = "INSERT INTO group_user_left (group_user_id, group_puid, user_puid) VALUES(%s, %s, %s)"
    sqlDel = "delete gu from group_user gu where group_puid = %s and user_puid = %s"

    try:
        cursor = getCursor(dbd)
        cursor.execute(sqlAdd,(groupUser['id'], groupPuid, userPuid))
        cursor.execute(sqlDel,(groupPuid, userPuid))
        dbd.db.commit()
        cursor.close()
    except Exception as e:
        dbd.db.rollback()  # 事务回滚
        dealException()
        logError('dbDelGroupUse err: '+str(e))
        return False
    
    return True